/*
 * Copyright 2019 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.mlkit.samples.nl.translate.java;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.mlkit.samples.nl.translate.AppExecutor;
import com.google.mlkit.samples.nl.translate.AssetCopyHelper;
import com.google.mlkit.samples.nl.translate.FileCopyHelper;
import com.google.mlkit.samples.nl.translate.R;

public class MainActivity extends AppCompatActivity {

  @RequiresApi(api = Build.VERSION_CODES.N)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), (v, insets) -> {
      Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
      v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
      return insets;
    });

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_translate_main);

    if (savedInstanceState == null) {
      getSupportFragmentManager()
          .beginTransaction()
          .replace(R.id.container, TranslateFragment.newInstance())
          .commitNow();
    }

    //backup
//    if (FileCopyHelper.checkStoragePermission(this)) {
//      copyFiles();
//    } else {
//      FileCopyHelper.requestStoragePermission(this);
//    }

    //restore
    AppExecutor.obtain().submit(() -> {
      boolean success = AssetCopyHelper.copyTranslateModels(this);
      runOnUiThread(() -> {
        if (success) {
          Toast.makeText(this, "Files copied successfully", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(this, "Failed to copy files", Toast.LENGTH_SHORT).show();
        }
      });
    });

  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == FileCopyHelper.STORAGE_PERMISSION_CODE) {
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        copyFiles();
      }
    }
  }

  private void copyFiles() {
    AppExecutor.obtain().submit(() -> {
      boolean success = FileCopyHelper.copyInternalToExternal(
              this,
              "no_backup/com.google.mlkit.translate.models",
              "no_backup/com.google.mlkit.translate.models"
      );

      runOnUiThread(() -> {
        if (success) {
          Toast.makeText(this, "Files copied successfully", Toast.LENGTH_SHORT).show();
        } else {
          Toast.makeText(this, "Failed to copy files", Toast.LENGTH_SHORT).show();
        }
      });
    });
  }
}
